/**
 * Created by Administrator on 2017/3/24 0024.
 */
var Page1={
    create:function () {
        var page={};
        var idx=-1;
        var bool_touch;
        var bool_playing;
        var bool_open;
        page.ui=new PIXI.DisplayObjectContainer();

        var bg=new PIXI.Sprite(resource['assets/images/p1_bg.png'].texture);
        page.ui.addChild(bg);

        // var t0=new PIXI.Sprite(resource['p0_t0.png'].texture);
        // t0.x=0;
        // t0.y=0;
        // page.ui.addChild(t0);
        //
        // var t1=new PIXI.Sprite(resource['p0_t1.png'].texture);
        // t1.x=0;
        // t1.y=0;
        // page.ui.addChild(t1);

        // var arr_ablum=[];
        // for(var i=0;i<6;i++)
        // {
        //     var sp=new PIXI.Sprite(resource['p1_i'+i.toString()+'.png'].texture);
        //     sp.anchor.set(0.5);
        //     sp.x=0;
        //     sp.y=0;
        // }
        var startRot=0;

        var t0=new PIXI.Sprite(resource['assets/images/p1_t0.png'].texture);
        t0.anchor.set(0.5);
        t0.x=366;
        t0.y=43;
        page.ui.addChild(t0);

        var line=new PIXI.Sprite(resource['assets/images/p1_line.png'].texture);
        line.anchor.set(0.5);
        line.x=364;
        line.y=82;
        page.ui.addChild(line);

        var con=new PIXI.DisplayObjectContainer();
        page.ui.addChild(con);
        con.x=370;
        con.y=580;

        var cover=new PIXI.Sprite(resource['assets/images/p1_cover.png'].texture);
        cover.anchor.set(0.5);
        cover.x=0;
        cover.y=0;
        con.addChild(cover);

        var sp_ablum=new PIXI.Sprite(resource['assets/images/p1_a0.png'].texture);
        sp_ablum.scale.x=sp_ablum.scale.y=1;
        sp_ablum.anchor.set(0.5);
        sp_ablum.x=0;
        sp_ablum.y=0;
        con.addChild(sp_ablum);

        var sp_ablumText=new PIXI.Sprite(resource['assets/images/p1_tt0.png'].texture);
        // sp_ablum.scale.x=sp_ablum.scale.y=0.75;
        sp_ablumText.anchor.set(0.5);
        sp_ablumText.x=372;
        sp_ablumText.y=945;
        page.ui.addChild(sp_ablumText);

        // var gp_circle=new PIXI.Graphics();
        // gp_circle.beginFill(0x0);
        // gp_circle.drawCircle(0,0,395);
        // gp_circle.endFill();
        // con.addChild(gp_circle);

        // sp_ablum.mask=gp_circle;

        page.ui.addChild(con);

        var needle=new PIXI.Sprite(resource['assets/images/p1_needle.png'].texture);
        needle.name='next';
        needle.anchor.set(51/215,88/393);
        needle.x=377;
        needle.y=182;
        needle.rotation=-33*Math.PI/180
        page.ui.addChild(needle);

        var sp_next=new PIXI.Sprite(resource['assets/images/p1_next.png'].texture);
        sp_next.interactive=true;
        sp_next.name='next';
        sp_next.anchor.set(0.5);
        sp_next.x=544;
        sp_next.y=1040;
        page.ui.addChild(sp_next);
        sp_next.on('pointerdown',downHandler);

        var sp_pre=new PIXI.Sprite(resource['assets/images/p1_pre.png'].texture);
        sp_pre.interactive=true;
        sp_pre.anchor.set(0.5);
        sp_pre.name='pre';
        sp_pre.alpha=0.5;
        sp_pre.x=207;
        sp_pre.y=1040;
        page.ui.addChild(sp_pre);
        sp_pre.on('pointerdown',downHandler);

        var sp_stop=new PIXI.Sprite(resource['assets/images/p1_stop.png'].texture);
        sp_stop.interactive=true;
        sp_stop.anchor.set(0.5);
        sp_stop.name='stop';
        sp_stop.x=375;
        sp_stop.y=1040;
        // page.ui.addChild(sp_stop);
        sp_stop.on('pointerdown',downHandler);

        var sp_play=new PIXI.Sprite(resource['assets/images/p1_play.png'].texture);
        sp_play.interactive=true;
        sp_play.anchor.set(0.5);
        sp_play.name='play';
        sp_play.x=375;
        sp_play.y=1040;
        page.ui.addChild(sp_play);
        sp_play.on('pointerdown',downHandler);

        var sp_list=new PIXI.Sprite(resource['assets/images/p1_list.png'].texture);
        sp_list.interactive=true;
        sp_list.anchor.set(0.5);
        sp_list.name='list';
        sp_list.x=687;
        sp_list.y=1045;
        sp_list.scale.x=sp_list.scale.y=0;
        page.ui.addChild(sp_list);
        sp_list.on('pointerdown',downHandler);

        var con_list=new PIXI.DisplayObjectContainer();
        var arr_list=[];
        for(var i=0;i<7;i++)
        {
            var bt_list=new PIXI.Sprite(resource['assets/images/p1_list'+i.toString()+'.png'].texture);
            bt_list.interactive=true;
            bt_list.name=i.toString();
            bt_list.x=0;
            bt_list.y=108*i;
            con_list.addChild(bt_list);
            bt_list.on('pointerdown',listDownHandler);
            arr_list.push(bt_list);
        }
        con_list.x=0;
        con_list.y=375;
        // page.ui.addChild(con_list);

        function listDownHandler(e) {
            // openList();
            if(bool_open) {

                if (e.currentTarget.name.toString() != '6') {
                    // idx = e.currentTarget.name.toString();
                    if (!bool_playing) {
                        startPlay();
                    }
                    // setTimeout(function () {
                    //
                    // })
                    if(idx!=e.currentTarget.name.toString())
                    {
                        idx=e.currentTarget.name.toString();
                        changeAudio();
                    }
                }
                // changeAudio();

                closeList(e.currentTarget.name.toString());
            }
        }
        function openList()
        {
            if(!bool_open)
            {
                if(sp_list.parent)
                {
                    sp_list.parent.removeChild(sp_list);
                }

                page.ui.addChild(con_list);
                bool_open=true;
                // TweenMax.to()
                for(var i=0;i<arr_list.length;i++)
                {

                    TweenMax.killTweensOf(arr_list[i]);
                    arr_list[i].x=-740;
                    arr_list[i].alpha=0;
                    if(i==idx||i=='6')
                    {
                        TweenMax.to(arr_list[i],0.5,{delay:i*0.1,x:0,alpha:1,ease:Back.easeOut});
                    }
                    else
                    {
                        TweenMax.to(arr_list[i],0.5,{delay:i*0.1,x:0,alpha:0.7,ease:Back.easeOut});
                    }

                }
            }

        }
        function closeList(_idx)
        {
            if(bool_open) {


                for(var i=0;i<arr_list.length;i++)
                {
                    // arr_list[i].x=0-100;
                    // arr_list[i].alpha=0;
                    TweenMax.to(arr_list[i],0.5,{delay:0.5+i*0.1,x:740,alpha:0,ease:Back.easeIn});
                }
                setTimeout(function () {
                    if(con_list.parent)
                    {
                        bool_open = false;
                        con_list.parent.removeChild(con_list);
                        page.ui.addChild(sp_list);
                        sp_list.scale.x=sp_list.scale.y=0;
                        TweenMax.to(sp_list.scale,1,{delay:0,x:1,y:1,ease:Back.easeOut});
                    }
                    // if(idx!=_idx)
                    // {
                    //     idx=_idx;
                    //     changeAudio();
                    // }

                },2000)
            }
        }

        page.In=function () {
            bg.alpha=0;
            TweenMax.to(bg,1,{alpha:1,ease:Cubic.easeInOut});
            line.scale.x=line.scale.y=0;
            TweenMax.to(line.scale,2,{delay:1.2,x:1,y:1,ease:Linear.easeNone});
            t0.alpha=0;
            t0.y=43+50;
            TweenMax.to(t0,1,{delay:1.5,alpha:1,y:43,ease:Cubic.easeInOut});

            con.scale.x=con.scale.y=0;
            TweenMax.to(con.scale,1,{delay:2,x:1,y:1,ease:Back.easeOut});

            sp_ablumText.alpha=0;
            TweenMax.to(sp_ablumText,1,{delay:2,alpha:1,ease:Back.easeOut});

            needle.alpha=0;
            TweenMax.to(needle,1,{delay:3.5,alpha:1,ease:Back.easeOut});



            sp_pre.scale.x=sp_pre.scale.y=0;
            TweenMax.to(sp_pre.scale,1,{delay:2.5,x:1,y:1,ease:Back.easeOut});
            sp_next.scale.x=sp_next.scale.y=0;
            TweenMax.to(sp_next.scale,1,{delay:2.7,x:1,y:1,ease:Back.easeOut});
            sp_play.scale.x=sp_play.scale.y=0;
            TweenMax.to(sp_play.scale,1,{delay:2.9,x:1,y:1,ease:Back.easeOut});
            sp_list.scale.x=sp_list.scale.y=0;
            TweenMax.to(sp_list.scale,1,{delay:3.1,x:1,y:1,ease:Back.easeOut});
        };
        page.Out=function () {

        };
        var tw;
        var tw1;
        function startPlay()
        {
            bool_playing=true;
            TweenMax.killTweensOf(con);
            TweenMax.killTweensOf(needle);

            //唱片
            TweenMax.to(con,10,{delay:0.8,rotation:startRot+Math.PI*2,alpha:1,repeat:-1,ease:Linear.easeNone});

            //按钮区
            if(sp_play.parent)
            {
                sp_play.parent.removeChild(sp_play);
            }
            page.ui.addChild(sp_stop);
            //唱针
            tw=TweenMax.to(needle,0.7,{rotation:0*Math.PI/180,ease:Back.easeInOut});
            tw1=TweenMax.to(needle.skew,1,{x:0.1,ease:Cubic.easeInOut,repeat:-1,yoyo:true});
        }
        function endPlay()
        {
            bool_playing=false;
            //唱片
            // tw.pause();
            TweenMax.killTweensOf(con);
            TweenMax.killTweensOf(needle);
            startRot=con.rotation;
            //按钮区

            if(sp_stop.parent)
            {
                sp_stop.parent.removeChild(sp_stop);
            }
            page.ui.addChild(sp_play);
            //唱针
            TweenMax.to(needle,0.7,{rotation:-33*Math.PI/180,ease:Cubic.easeOut});
        }


        function changeAudio()
        {
            console.log(idx);
            TweenMax.to(con.scale,0.5,{x:0.95,y:0.95,ease:Cubic.easeOut});
            TweenMax.to(con.scale,0.5,{delay:0.5,x:1,y:1,ease:Back.easeOut});

            TweenMax.to(con,0.5,{alpha:0.5,onComplete:function () {
                console.log('assets/images/p1_a'+idx.toString()+'.png')
                sp_ablum.texture=resource['assets/images/p1_a'+idx.toString()+'.png'].texture;
                sp_ablumText.texture=resource['assets/images/p1_tt'+idx.toString()+'.png'].texture;
            }});

            // sp_ablumText.alpha=0;
            // TweenMax.to(sp_ablumText,1,{delay:2,alpha:1,ease:Back.easeOut});

            musicControl.zlmPlay(idx);
            TweenMax.to(con,0.5,{delay:0.5,alpha:1});
        }

        function downHandler(e)
        {
            console.log(e.currentTarget.name)
            if(bool_open)
            {
                return;
            }
            if(e.currentTarget.name=='next')
            {
                // if(idx==-1) {
                //     sp_list.scale.x = sp_list.scale.y = 0;
                //     TweenMax.to(sp_list.scale, 1, {delay: 1, x: 1, y: 1, ease: Back.easeOut});
                // }
                if(idx==-1)
                {
                    idx=0;
                }
                if(idx<5)
                {

                    idx++;
                    if(idx==5)
                    {
                        sp_next.alpha=0.5;
                    }
                    sp_pre.alpha=1;
                    if(!bool_playing)
                    {
                        startPlay();
                    }
                    console.log('next::',idx);
                    changeAudio();
                }
                TweenMax.to(sp_next.scale,0.3,{x:0.3,y:0.7});
                TweenMax.to(sp_next.scale,0.3,{delay:0.3,x:1,y:1});
            }

            else if(e.currentTarget.name=='pre')
            {
                // if(idx==-1) {
                //     sp_list.scale.x = sp_list.scale.y = 0;
                //     TweenMax.to(sp_list.scale, 1, {delay: 1, x: 1, y: 1, ease: Back.easeOut});
                // }
                if(idx==-1)
                {
                    idx=0;
                }
                if(idx>0)
                {
                    idx--;
                    if(idx==0)
                    {
                        sp_pre.alpha=0.5;
                    }
                    sp_next.alpha=1;
                    if(!bool_playing)
                    {
                        startPlay();
                    }
                    changeAudio();
                }
                TweenMax.to(sp_pre.scale,0.3,{x:0.7,y:0.7});
                TweenMax.to(sp_pre.scale,0.3,{delay:0.3,x:1,y:1});
            }
            else if(e.currentTarget.name=='stop')
            {
                endPlay();
                musicControl.zlmPause();
            }
            else if(e.currentTarget.name=='play')
            {
                if(idx==-1)
                {
                    // sp_list.scale.x=sp_list.scale.y=0;
                    // TweenMax.to(sp_list.scale,1,{delay:1,x:1,y:1,ease:Back.easeOut});
                    idx=0;
                    changeAudio();
                    startPlay();
                }
                else
                {
                    startPlay();
                    musicControl.zlmResume();
                }
            }
            else if(e.currentTarget.name=='list')
            {

                openList();
            }
        }


        return page;
    }
}